<?php

class Admin_Model_Posts{

	private $postTbl;
	
	public function __construct(){
		$this->postTbl		= new Admin_Model_DbTable_Posts();
	}
	
	public function selectPost($where = NULL,$order ='baiVietId DESC',$limit=null){
		$dbAdapter = Zend_Db_Table::getDefaultAdapter();
		$dbSelect  = new Zend_Db_Select($dbAdapter);
		
		$dbSelect->from(array('bv'=>'baiViet'),'*')
					->joinLeft(
						array('m2'=>'menu2'), 
						'm2.menu2Id = bv.menu2Id',
						array('tenMenu2','menu1Id','dangKyDV')
					);
					
		if($where !=null){
			$dbSelect->where($where);
		}
		
		$dbSelect->order($order);
		
		if($limit!=null) $dbSelect->limit($limit);
		
		return $dbSelect;
	}
	
	/**
	 * 
	 * Them bai viet moi
	 * @param unknown_type $tieuDe
	 * @param unknown_type $hinh
	 * @param unknown_type $noiDung
	 * @param unknown_type $ngayPost
	 * @param unknown_type $mucId
	 */
	public function addPost($tieuDe, $hinh, $noiDung, $ngayViet, $menu2Id,$trangThai=0, $tenDangNhap	){
		$data = array(
			'tieuDe'		=> $tieuDe,
			'hinh'			=> $hinh,
			'noiDung'		=> $noiDung,
			'ngayViet'		=> $ngayViet,
			'menu2Id'		=> $menu2Id,
			'trangThai'		=> $trangThai,
			'tenDangNhap'	=> $tenDangNhap	
		);
		
		$this->postTbl->insert($data);
	}
	
	public function addCustomPost($data){
		$this->postTbl->insert($data);
		return  $this->postTbl->getAdapter()->lastInsertId('baiviet','baiVietId');
	}
	
	public function updatePost($tieuDe, $hinh, $noiDung, $ngayViet, $menu2Id,$trangThai=0, $tenDangNhap, $baiVietId	){
		$data = array(
			'tieuDe'		=> $tieuDe,
			'hinh'			=> $hinh,
			'noiDung'		=> $noiDung,
			'ngayViet'		=> $ngayViet,
			'menu2Id'		=> $menu2Id,
			'trangThai'		=> $trangThai,
			'tenDangNhap'	=> $tenDangNhap	
		);
		
		$this->postTbl->update($data, "baiVietId =".$baiVietId);
	}
	
	public function updateCustomPost($data,$where){
		//
		//	=== Update record in Db
		$this->postTbl->update($data, $where);
		
		//
		//	=== Update recor in Lucene Docs
		//$hits	= $indexSearch->find('new_id:'.)
	}
	
	public function deletePost($postId){
		//
		//	=== Del Recor in Db
		$this->postTbl->delete("baiVietId = ".$postId);
		
		//
		//	=== Xoa record trong Doc cua Lucene da dc tao ra truoc do
		

		try {
//			$indexSearch = Zend_Search_Lucene::open('docs');
//
//			$hits = $indexSearch->find('new_id:'.$postId);
//			foreach ($hits as $hit) {
//				$indexSearch->delete($hit->baiVietId);
//			}
		}
		catch (Exception $e){
			
		}
	}
	
	public function fetchRowPost($baiVietID){
		return $this->postTbl->fetchRow("baiVietId =".$baiVietID);
	}
	
	public function fetchAllPosts($where=NULL){
		return $this->postTbl->fetchAll($where=null);
	}
	
	/**
	 * 
	 * Lay du lieu cua tat ca cac cot trong array column
	 * @param unknown_type $colArr: danh sach cac cot
	 */
	public function fetchCol($colArr,$where=NULL){
		$sql = "SELECT ";
		for($i =0; $i<count($colArr); $i++){
			if($i != count($colArr)-1)
				$sql .= " ".$colArr[$i]."," ;
			else 
				$sql .= " ".$colArr[$i];
		}
		$sql .=" FROM baiViet ";
		
		if ($where!=NULL){
			$sql .= " WHERE ".$where;	
		}
		return $this->postTbl
					->getAdapter()
					->fetchAll($sql);
	}
	
	//*******************************************/
	// Tìm kiếm với zend_search_lucence
	public function searchItem($arrPara = null, $option = NULL){
		$keywork = 'test';
		$indexSearch = Zend_Search_Lucene::open('docs');
		if(!empty($keywork)){
			$hits = $indexSearch->find($keywork);
			return $hits;
		}
	}
	
	//	===============================
	//	== create document if is not exist, add
	
	public function saveAddItem($arrPara = null, $option=NULL){
		if($option['task'] == 'add'){
			try {
				$indexSearch = Zend_Search_Lucene::open('docs');
			} catch (Exception $e) {
				$indexSearch = Zend_Search_Lucene::create('docs');
			}
			
			// Tao file doc luu toan bo thong tin
			$doc = new Zend_Search_Lucene_Document();

			$doc->addField(Zend_Search_Lucene_Field::keyword('new_id', $arrPara['baiVietId']))
				->addField(Zend_Search_Lucene_Field::text('tieude', $arrPara['tieuDe']))
				->addField(Zend_Search_Lucene_Field::text('noiDung', $arrPara['noiDung']));
			
			$indexSearch->addDocument($doc);
			$indexSearch->commit();
			$indexSearch->optimize();
		}
		if($option['task'] == 'edit'){
			try {
				$indexSearch = Zend_Search_Lucene::open('docs');
			} catch (Exception $e) {
				$indexSearch = Zend_Search_Lucene::create('docs');
			}
			
			$hits	= $indexSearch->find('new_id:'. $arrPara['baiVietId']);
			foreach ($hits as $hit) {
				$indexSearch->delete($hit->baiVietId);
			}
			
			$doc	= new Zend_Search_Lucene_Document();
			$doc->addField(Zend_Search_Lucene_Field::keyword('new_id', $arrPara['baiVietId']))
				->addField(Zend_Search_Lucene_Field::text('tieuDe', $arrPara['tieuDe']))
				->addField(Zend_Search_Lucene_Field::text('noiDung', $arrPara['noiDung']));
				
			$indexSearch->addDocument($doc);
			$indexSearch->commit();
			$indexSearch->optimize();
			
		}
	}  
}

